Ubuntu Linux 使用 timedatectl 校正時間、時區教學與範例 |
您所在的位置:网站首页 › timedatectl set-ntp no › Ubuntu Linux 使用 timedatectl 校正時間、時區教學與範例 |
介紹如何在 Ubuntu Linux 中使用 timedatectl 透過網路 NTP 或手動校正系統時間。 timedatectl 時間管理工具 timedatectl 是一個 Ubuntu Linux 預設的系統時間管理工具,支援網路校時與手動校時。直接執行該指令,可以顯示目前的設定狀態: # 顯示目前狀態 timedatectl Local time: Mon 2020-04-06 18:11:32 CST Universal time: Mon 2020-04-06 10:11:32 UTC RTC time: Mon 2020-04-06 10:11:33 Time zone: Asia/Taipei (CST, +0800) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: no 網路校時timedatectl 的 set-ntp 功能可以設定讓系統自動透過網路校時(NTP): # 啟用 NTP 校時 timedatectl set-ntp yes正常來說,Ubuntu Linux 預設就會開啟網路校時功能。 timedatectl 的網路校時功能是透過 systemd-timesyncd 服務來處理的,在啟用網路校時之後,該服務就會自動啟動,我們可以用以下指令檢查 systemd-timesyncd 服務的狀態: # 檢查 systemd-timesyncd 服務狀態 systemctl status systemd-timesyncd ● systemd-timesyncd.service - Network Time Synchronization Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendo Active: active (running) since Mon 2020-04-06 18:35:20 CST; 23min ago Docs: man:systemd-timesyncd.service(8) Main PID: 5162 (systemd-timesyn) Status: "Synchronized to time server 91.189.89.198:123 (ntp.ubuntu.com)." Tasks: 2 (limit: 4915) CGroup: /system.slice/systemd-timesyncd.service └─5162 /lib/systemd/systemd-timesyncd如果想要自行指定網路校時的 NTP 伺服器,可以編輯 /etc/systemd/timesyncd.conf 設定檔,以 NTP 參數來設定要使用的 NTP 伺服器: [Time] # NTP 伺服器(以空白分隔多個伺服器) NTP=tw.pool.ntp.org jp.pool.ntp.org # 備用 NTP 伺服器(以空白分隔多個伺服器) FallbackNTP=sg.pool.ntp.org ntp.ubuntu.com更改了 NTP 伺服器的設定之後,要重新啟動 systemd-timesyncd 服務,新的設定才會生效: # 重新啟動 systemd-timesyncd 服務 systemctl restart systemd-timesyncd若要關閉自動網路校時,則執行: # 停用 NTP 校時 timedatectl set-ntp no 手動校時如果在沒有網路的狀態下,要以手動輸入時間的方式校正系統時間,可以使用 timedatectl 的 set-time 功能來設定日期與時間: # 手動校時 timedatectl set-time "2020-04-06 18:17:16" 查詢時區設定有時候伺服器的時區設定錯誤,也會造成時間的錯亂,最常見的問題提就是剛安裝好的伺服器沒有進行時區的設定,使用預設的 UTC 時區,這時候系統的時間就會跟本地時間差很多。 時區設定可以從 timedatectl 指令的輸出中查看,若設定成 UTC 時區的話,Time zone 會顯示為 UTC,類似這樣: # 顯示目前時區設定 timedatectl Local time: Thu 2021-10-07 07:06:21 UTC Universal time: Thu 2021-10-07 07:06:21 UTC RTC time: Thu 2021-10-07 07:06:21 Time zone: Etc/UTC (UTC, +0000) System clock synchronized: yes NTP service: active RTC in local TZ: no系統的時區設定是透過 /etc/localtime 這個檔案來設定的,我們也可以透過這個檔案來查看時區設定: # 查看系統時區設定檔 ls -l /etc/localtime lrwxrwxrwx 1 root root 27 Oct 7 06:17 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC另外系統的時區資訊也會寫在 /etc/timezone 這個檔案中: # 查看系統時區設定 cat /etc/timezone Etc/UTC 更改時區設定若要更改時區設定,首先要查詢系統上可用的時區選項: # 查詢系統時區設定選項 timedatectl list-timezones由於選項很多,可以搭配 grep 查詢自己的城市: # 查詢系統時區設定選項 timedatectl list-timezones | grep Taipei Asia/Taipei查到自己的時區資訊之後,再以 timedatectl 更改系統的時區設定: # 更改系統時區設定 sudo timedatectl set-timezone Asia/Taipei最後再確認一下系統時區資訊: # 顯示目前時區設定 timedatectl Local time: Thu 2021-10-07 15:19:26 CST Universal time: Thu 2021-10-07 07:19:26 UTC RTC time: Thu 2021-10-07 07:19:26 Time zone: Asia/Taipei (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no 參考資料 Linuxize:How To Set or Change Timezone on Ubuntu 20.04 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |